/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sengine;

import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.*;

public class Sprite extends Object2D {
    private Texture texture;
    private SpriteEngine sengine;
    
    public Sprite(int x, int y, int width, int height) {
        super(x, y, width, height);
    }
    
    public Texture getTexture() {
        return texture;
    }

    public void setTexture(Texture texture) {
        this.texture = texture;
    }
    
    void setSEngine(SpriteEngine sengine) {
        if(this.sengine != null && sengine != null) {
            throw new IllegalStateException("Trying to set sengine twice");
        }
        this.sengine = sengine;
    }
    
    protected void rmSelf() {
        this.sengine.rmSprite(this);
    }
    
    protected void addSprite(Sprite sprite) {
        this.sengine.addSprite(sprite);
    }
    
    public void render() {
        texture.activate();
        
        int width = (int) getWidth();
        int height = (int) getHeight();
        
        glPushMatrix();
        {
            // Translate to the location.
            glTranslatef(getX()+sengine.getView(), getY(), 0);
            
            GL11.glRotatef(getR(), 0, 0, 1);

            // Begin drawing with QUADS. A QUAD is a polygon with four vertices.
            glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
            glTexCoord2f(0, 1);
            glVertex2f(0, height);
            glTexCoord2f(1, 1);
            glVertex2f(width, height);
            glTexCoord2f(1, 0);
            glVertex2f(width, 0);
            glEnd();  // End the drawing.
        }
        glPopMatrix();
    }
}
